﻿using NCS.IConnect.Core.Utilities;
using NCS.IConnect.Core.WebApi.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NCS.IConnect.Core.WebApi.Description
{
    /// <summary>
    /// This attribute is used to specify a method to initialize sample data for web api documentation generation.
    /// </summary>
    [AttributeUsage( AttributeTargets.Assembly)]
    public class SampleDataInitializeAttribute: Attribute
    {
        /// <summary>
        /// The <see cref="MethodInfo"/> to initialize sample data.
        /// </summary>
        public MethodInfo Method { get; private set; }

        /// <summary>
        /// Create a new <see cref="SampleDataInitializeAttribute"/>.
        /// </summary>
        /// <param name="type">The type of initialize sample data.</param>
        /// <param name="method">The name of method to initialize sample data.</param>
        public SampleDataInitializeAttribute(Type type, string method)
        {
            Guard.ArgumentNotNull(type, "type");
            Guard.ArgumentNotNullOrEmpty(method, "method");

            this.Method = type.GetMethod(method);
            if (null == this.Method)
            {
                throw new InvalidOperationException(Resources.ExceptionMethodNotFound.Format(new object[]{method, type.FullName}));
            }

            var parameters = this.Method.GetParameters();
            if (parameters.Length != 1 && parameters[0].ParameterType != typeof(ISampleRegister))
            {
                throw new InvalidOperationException(Resources.ExceptionInvalidInitializeMethod.Format(new object[] { method, type.FullName }));
            }
        }
    }
}
